home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-08-23 | 2.9 KB | 124 lines | [TEXT/MPS ] |
- // PtrObject.cp
- #include <Memory.h>
- #include <Errors.h>
- #include <stdio.h>
- #include <stddef.h>
-
- #include "PtrObject.h"
-
- // Static data members actually need to be declared outside of the class definition in
- // order to have space allocated.
- THz PtrObject::fZone = nil;
-
-
- OSErr PtrObject::AllocHeap(size_t heapSize)
- {
- // By default, the heap gets kNumDfltMasters master pointers. A small number, but it
- // shouldn’t matter, since we will only be allocating Ptrs in this heap, and Ptrs
- // don’t use master pointers.
- const short kNumDfltMasters = 16;
-
- // This magic number from Inside Mac, vol. II, chapter 1, is the amount of space
- // required for the zone header and trailer, and the master pointer block. We add this
- // to the requested heap size to compensate.
- const size_t kZoneOverhead = 64 + 8 + (sizeof(long) * kNumDfltMasters);
-
- heapSize += kZoneOverhead; // Factor in overhead.
-
- // Allocate space for the zone.
- Ptr zonePtr = NewPtr(heapSize);
- if (!zonePtr) // if alloc fails, return error
- return MemError(); // Get a pointer to the end of the heap.
-
- Ptr limitPtr = (Ptr) (((ptrdiff_t) zonePtr) + heapSize);
-
- // Initialize the zone.
- THz savedZone = GetZone();
- InitZone(nil, kNumDfltMasters, limitPtr, zonePtr);
- SetZone(savedZone);
-
- // Save the zone pointer in our static class variable.
- fZone = (THz) zonePtr;
- return noErr;
- }
-
- void PtrObject::DisposeHeap()
- {
- // If zone actually exists, dispose of it.
- if (fZone)
- {
- DisposPtr((Ptr) fZone);
- fZone = nil;
- }
- }
-
- long PtrObject::FreeMemory()
- {
- THz savedZone;
-
- // Before we can return the amount of free memory, we
- // need to switch to the correct zone.
- if (fZone)
- {
- savedZone = GetZone(); // Save current zone.
- SetZone(fZone); // Make our zone current.
- }
-
- long free = FreeMem(); // Get total free space.
-
- if (fZone)
- SetZone(savedZone); // Restore previous zone.
-
- return free;
- }
-
- Size PtrObject::MaxMemory()
- {
- THz savedZone;
-
- // Before we can return the maximum block size, we need
- // to switch to the correct zone.
- if (fZone)
- {
- savedZone = GetZone(); // Save current zone.
- SetZone(fZone); // Make our zone current.
- }
-
- // We know the heap can’t grow, but we have to have a temp
- // variable to satisfy the Toolbox.
- Size tSize;
-
- Size max = MaxMem(&tSize); // Get size of biggest block.
-
- if (fZone)
- SetZone(savedZone); // Restore previous zone.
-
- return max;
- }
-
- void* PtrObject::operator new(size_t size)
- {
- THz savedZone;
-
- // before we can allocate memory, we need
- // to switch to the correct zone
- if (fZone)
- {
- savedZone = GetZone(); // Save current zone.
- SetZone(fZone); // Make our zone current.
- }
-
- Ptr p = NewPtr(size); // Allocate memory for object.
-
- if (fZone)
- SetZone(savedZone); // Restore previous zone.
-
- return p;
- }
-
- void PtrObject::operator delete(void* p)
- {
- DisposPtr((Ptr) p); // This works regardless of the zone
- // the pointer was allocated in.
- }
-